package com.kaiyidiannao.model.req.model;

import android.text.TextUtils;
import android.util.Log;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.model.Result;
import com.kaiyidiannao.model.bean.DaydayCouponBean;
import com.kaiyidiannao.model.bean.PlayBean;
import com.kaiyidiannao.model.req.base.BaseCommand;
import com.taplinker.core.util.json.JsonUtil;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by wzg on 14-4-24.
 * 玩乐住，美食城， 玩乐住 详细请求页面
 */
public class GetFoodPalaceShow extends BaseCommand {
    private int itemid;
    private int page = DataCenter.LISTVIEW_DEFAULT_PAGE; //当前页码
    private String userid;
    private String TAG = "GetFoodPalaceShow";
    private String catid;

    public GetFoodPalaceShow(int itemid, String catid) {
        this.itemid = itemid;
        this.catid = catid;
    }

    public GetFoodPalaceShow(int itemid, String userid, String catid) {
        this.itemid = itemid;
        this.userid = userid;
        this.catid = catid;
    }

    @Override
    public String getRequestUrl() {
        return DataCenter.GET_PLAY_DETAIL_ADDRESS;
    }

    @Override
    public HashMap<String, String> getRequestParamters() {
        HashMap<String, String> paramters = new HashMap<String, String>();
        paramters.put("itemid", itemid + "");
        if (!TextUtils.isEmpty(userid)) {
            paramters.put("userid", userid);
        }
        if (!TextUtils.isEmpty(catid)) {
            paramters.put("catid", catid);
        }
        return paramters;
    }

    @Override
    public Result go(String url, HashMap<String, String> paramters) { // 联网时后台执行
        // 根据请求参数返回请求数据
        return HttpUtils.getByHttpClient(url, paramters);
    }

    @Override
    public void yes(Result result) { //后台执行完毕后执行
        super.yes(result);
        Log.d(DataCenter.LOG_INFO, "GetCaseComment.yes()");

        JSONArray jsonArray = null;
        try {
            Log.d(TAG, result.object.toString());

            JSONObject jsonObject = new JSONObject(result.object.toString());
            JSONObject obj = jsonObject.getJSONObject("data");
            PlayBean bean = JsonUtil.jsonToObject(obj.toString(), PlayBean.class);

            ArrayList<PlayBean> targetList = DataCenter.foodPalaceList;
            if (page == DataCenter.LISTVIEW_DEFAULT_PAGE) {
                targetList.clear();
            }
            targetList.add(bean);

            Log.d(TAG, targetList.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

        DataCenter.getInstance().callBacks(DataCenter.flag.GET_PLAY_SHOW_SUCCESS);

        if (jsonArray != null && jsonArray.length() < 1) {
            DataCenter.getInstance().callBacks(DataCenter.flag.GET_PLAY_SHOW_FAILED);
        }
    }

    @Override
    public void no(Result result) {
        super.no(result);
        Log.d(DataCenter.LOG_ERROR, "GetFoodPalaceShow.no()");

        DataCenter.playBeanList.clear();
        DataCenter.getInstance().callBacks(DataCenter.flag.GET_PLAY_SHOW_FAILED);

    }
}
